package com.hdw.auth.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hdw.auth.api.RolesService;
import com.hdw.auth.mapper.SysRolesMapper;
import com.hdw.auth.mapper.SysUsersRolesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hdw.auth.bean.vo.LayMsg;
import com.hdw.auth.bean.vo.Msg;
import com.hdw.auth.bean.model.SysRoles;
import com.hdw.auth.bean.model.SysUsers;
import com.hdw.auth.bean.model.SysUsersRoles;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author GuoPeng
 * @Title: RolesServiceImpl
 * @Description:
 * @date 2019/5/11 10:30
 */
@Service
@Transactional
public class RolesServiceImpl implements RolesService {


    @Resource
    private SysRolesMapper sysRolesMapper;

    @Resource
    private SysUsersRolesMapper sysUsersRolesMapper;

    @Override
    public LayMsg getRolesList(Integer page, Integer limit) {
            PageHelper.startPage(page,limit );
            Page<SysUsers> selectall = (Page)sysRolesMapper.selectall();
            return LayMsg.success().addCount(selectall.getTotal()).addData(selectall.getResult());
    }

    @Override
    public Msg updateRoles(SysRoles roles) {
        int i = sysRolesMapper.updateByPrimaryKeySelective(roles);
        if(i>0){
            return  Msg.success();
        }else {
            return  Msg.fail();
        }
    }

    @Override
    public Msg RolesLock(SysRoles roles) {
        int i = sysRolesMapper.updateByPrimaryKeySelective(roles);
        if(i>0){
            return  Msg.success();
        }else {
            return  Msg.fail();
        }
    }

    @Override
    public Msg delectRoles(Integer id) {

        int i = sysRolesMapper.deleteByPrimaryKey(id);
        if(i>0){
            return  Msg.success();
        }else {
            return  Msg.fail();
        }
    }

    @Override
    public Msg selectAll( Integer uid) {
        List<SysRoles> selectall = sysRolesMapper.selectall();
        List<SysUsersRoles> sysUsersRoles=null;
        ArrayList list = new ArrayList();
        HashMap parm = new HashMap();
        parm.put("user_id", uid);
        for (int n=0;n<selectall.size();n++) {
            sysUsersRoles = sysUsersRolesMapper.selectbyUseridAndRoleid(parm);
            for(int m=0;m< sysUsersRoles.size();m++){
                if(sysUsersRoles.get(m).getRoleId()==selectall.get(n).getId()){
                    HashMap data = new HashMap();
                    data.put("name", selectall.get(n).getRole());
                    data.put("id", selectall.get(n).getId());
                    data.put("star",true );
                    list.add(data);
                    selectall.remove(n);
                }
            }

        }

        for (int c=0;c<selectall.size();c++) {
            HashMap data = new HashMap();
            data.put("name", selectall.get(c).getRole());
            data.put("id", selectall.get(c).getId());
            data.put("star",false );
            list.add(data);
        }
        return  Msg.success().add("info", list);
    }

    @Override
    public Msg UserSetRoles(Map map, Integer id) {
       /*全部移除*/
        HashMap parm = new HashMap();
        parm.put("user_id", id);
        List<SysUsersRoles> sysUsersRoles2 = sysUsersRolesMapper.selectbyUseridAndRoleid(parm);
        for(int i=0;i<sysUsersRoles2.size();i++ ){
            sysUsersRolesMapper.deleteByPrimaryKey(sysUsersRoles2.get(i).getId());
        }


        Iterator it = map.entrySet().iterator();
        /*添加角色*/
         while (it.hasNext()){
             Map.Entry entry = (Map.Entry)it.next();
             HashMap parm0 = new HashMap();
             parm0.put("user_id", id);
             System.out.println(Integer.parseInt(entry.getKey().toString()));
             parm0.put("role_id", Integer.parseInt(entry.getKey().toString()));
             List<SysUsersRoles> sysUsersRoles1 = sysUsersRolesMapper.selectbyUseridAndRoleid(parm0);
             if(sysUsersRoles1.size()<1){
                 SysUsersRoles sysUsersRoles = new SysUsersRoles();
                 sysUsersRoles.setRoleId(Integer.parseInt(entry.getKey().toString()));
                 sysUsersRoles.setUserId(id);
                 int insert = sysUsersRolesMapper.insert(sysUsersRoles);
             }
         }

        return Msg.success();
    }

    @Override
    public Msg insertRoles(SysRoles roles) {
        int i = sysRolesMapper.insertSelective(roles);

        if(i>0){
            return Msg.success();
        }else {
            return Msg.fail();
        }
    }


}
